<?

define ("COLLECTION_OBJECT_ARRAY",1);
define ("COLLECTION_ID_ARRAY",2);

require_once("class_GenericRowSerial.php");
require_once("class_GenericObjectCollection.php");

/********************************************************************************************************/
/* This class represents one row from any table which hase non composit not autoincremented PRIMARY KEY */
/********************************************************************************************************/

class GenericMultiComposit extends GenericRowSerial {
    protected $composit_array; //In case it may be needed. But later change to "private"

    public function __construct($table_name, $pk, $my_id) {
	parent::__construct($table_name,$pk,$my_id);
	$this->composit_array=array();
	$this->last_condition_array=array();
    }

/***************** Configuration *****************************************************/

    public function add_composit($composit_table_name, $composit_class_name, $composit_link_fk, $composit_pk) {
/*
 * distinction between composited classes is based on table name.
 * $composit_class_name is then passed to composit's base class
 * $composit_link_fk is field in composit table which references to $this->pk
 */
	$this->composit_array[$composit_table_name]['collection']=new GenericObjectCollection($composit_table_name,$composit_class_name,$composit_pk);
	$this->composit_array[$composit_table_name]['link_fk']=$composit_link_fk;
    }

    public function initiate_composit($table_name,$condition_array) {
/*
 * array passed to this function in format: Condition=>Value,...
 * which should be parsed as:
 * <SELECT id FROM table_name WHERE ...> AND Condition='Value' ...)
 * Example: "<defailt_query> AND alive='t' AND observable='t'"
 * Parameter $content is an Enum of (COLLECTION_OBJECT_ARAY,COLLECTION_ID_ARRAY) defines what will be returned.
 */
	$string="SELECT ".$this->composit_array[$table_name]['collection']->get_pk()." FROM ".$table_name." WHERE ".$this->composit_array[$table_name]['link_fk']."=".$this->id;
	if (is_array($condition_array)) {
	    foreach ($condition_array as $cond=>$val) {
		$string.=" AND ".$cond."='".$val."'";
	    }
	}
	$this->sql->select_c($string);
	$this->composit_array[$table_name]['collection']->flush_array();
	foreach ($this->sql->get_result() as $new_id) {
	    $this->composit_array[$table_name]['collection']->add_id($new_id[$this->composit_array[$table_name]['collection']->get_pk()]);
	}
    }

    public function add_composit_object($table_name,$new_id) {
	if (isset($this->composit_array[$table_name])) {
	    $this->composit_array[$table_name]['collection']->add_id($new_id);
	}
    }

    public function remove_composit_object($table_name,$old_id) {
	if (isset($this->composit_array[$table_name])) {
	    $this->composit_array[$table_name]['collection']->remove_id($old_id);
	}
    }

/**************** Main Interface to this class *********************************************/

    public function get_composit($table_name,$content=COLLECTION_OBJECT_ARRAY) {
/*
 * Parameter $content is an Enum of (COLLECTION_OBJECT_ARAY,COLLECTION_ID_ARRAY) defines what will be returned.
 * COLLECTION_ID_ARRAY will return id array only
 */
	if (isset($this->composit_array[$table_name])) {
	    if ($content == COLLECTION_OBJECT_ARRAY) {
		return $this->composit_array[$table_name]['collection']->get_object_array();
	    } elseif ($content == COLLECTION_ID_ARRAY) {
		return $this->composit_array[$table_name]['collection']->get_id_array();
	    }
	}
	return false;
    }


    public function composit_obj_exists($table_name,$composit_field,$compare) {
/*
 * Finds object exists in composit class based on object field and value
 * Greps from existing pre-populated collection depends on last initiated condition. See notes for 'get_composit()'
 */

	foreach ($this->get_composit($table_name,COLLECTION_OBJECT_ARRAY) as $object) {
	    if ($object->get_field($composit_field) == $compare) {
		return $object->get_id();
	    }
	}
	return false;
    }


/*********** Private part ***************************************************************/
/*
    protected function _reload() {
	parent::_reload();
	foreach ($this->composit_array as $table_name) {
	    $this->_populate_composit_array($table);
	}
    }
*/



}

?>
